package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
	"path/filepath"
)

func main() {

	url := "https://dc03.iotdreamcatcher.net.cn:12448/v2/user/photo?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTA1NjgyNzMsInVpZCI6MzQ2NzYwLCJ6b25lIjoiVFMifQ.flGCQd4C05QGGm6Yztx1OCPdjmXZQacSNxQSfazJXG4&ptype=1&name=test123&sex=1&born=20201234&age=1&defa=1"
	method := "POST"

	payload := &bytes.Buffer{}
	writer := multipart.NewWriter(payload)
	file, errFile1 := os.Open("./image/SK-205.png")
	defer file.Close()
	part1, errFile1 := writer.CreateFormFile("image", filepath.Base("./image/SK-205.png"))
	_, errFile1 = io.Copy(part1, file)
	if errFile1 != nil {
		fmt.Println("http errFile", errFile1)
		return
	}
	err := writer.Close()
	if err != nil {
		fmt.Println("http write close", err)
		return
	}

	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		fmt.Println("http new req", err)
		return
	}
	req.Header.Add("Content-Type", "multipart/form-data")

	req.Header.Set("Content-Type", writer.FormDataContentType())
	res, err := client.Do(req)
	if err != nil {
		fmt.Println("http do", err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("http read", err)
		return
	}
	fmt.Println(string(body))
}
